package mytest.ssw.www.myandroidproject.activity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;

import mytest.ssw.www.myandroidproject.R;
import mytest.ssw.www.myandroidproject.mydate.Imageurl;
import mytest.ssw.www.myandroidproject.mydate.Userinfo;
import mytest.ssw.www.myandroidproject.thread.HttpPostThread;
import mytest.ssw.www.myandroidproject.thread.ThreadPoolUtils;

public class PictureWordActivity extends AppCompatActivity {
    private ListView pwliListView ;
    ProgressDialog dialog;
    Myadapter myadapter ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_word);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        pwliListView = (ListView)findViewById(R.id.pwlist);
         dialog = new ProgressDialog(this);
        myadapter  = new Myadapter(this);
        dialog.setTitle("提示");
        dialog.setMessage("正在加载");
        Myanytask myanytask = new Myanytask();
        myanytask.execute("http://www.weather.com.cn/adat/sk/101010100.html");
    }

class  Myanytask  extends AsyncTask<String,Void,Userinfo>
{
    @Override
    protected Userinfo doInBackground(String... params) {
        {
            Userinfo userinfo = new Userinfo();

            InputStream InputStream;
            try {
                URL url = new URL(params[0]);
                HttpURLConnection conn =  (HttpURLConnection)url.openConnection();
                conn.setConnectTimeout(6*1000);
                if (conn.getResponseCode()==200)
                {
                    InputStream is = conn.getInputStream();
                    Scanner scanner = new Scanner(is, "UTF-8");
                    String string = scanner.useDelimiter("\\A").next();
                    scanner.close();
                    Log.v("str", string);

                    JSONObject jsonObject = new JSONObject(string);
                    JSONObject tem = new JSONObject(jsonObject.getString("weatherinfo"));
                   userinfo.setUid(tem.getString("cityid"));
                    userinfo.setUname(tem.getString("city"));
                    userinfo.setUsex(tem.getString("WD"));
                    userinfo.setUphone(tem.getString("time"));
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }


            return userinfo;
        }
    }

    //onPreExecute方法用于在执行后台任务前做一些UI操作
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog.show();
    }


    //onProgressUpdate方法用于更新进度信息

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
    //onPostExecute方法用于在执行完后台任务后更新UI,显示结果
    @Override
    protected void onPostExecute(Userinfo userinfo) {
        super.onPostExecute(userinfo);
        myadapter.setdate(userinfo);
        pwliListView .setAdapter(myadapter);

        pwliListView.deferNotifyDataSetChanged();
        dialog.dismiss();
    }



}


    private class Myadapter extends BaseAdapter
    {

LayoutInflater layoutInflater;
      public   Userinfo userinfo;
        Imageurl imageurl = new Imageurl();
        String[] imageurls =imageurl.image;
        public Myadapter(Context content) {
            layoutInflater =layoutInflater.from(content);
        }
        public void setdate(Userinfo userinfo)
        {
            this.userinfo = userinfo;
        }

        @Override
        public int getCount() {
            return imageurls.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view =convertView;
            final   ViewHolder viewHolder ;
            if (view==null)
            { viewHolder = new ViewHolder();
               view = layoutInflater.inflate(R.layout.pwlistitem,null);
                 viewHolder.imageview=  (ImageView)view.findViewById(R.id.image);
                 viewHolder.textview = (TextView)view.findViewById(R.id.name);
                view.setTag(viewHolder);
            }
              else
            {
                viewHolder=(ViewHolder)view.getTag();
            }
            viewHolder.textview.setText(userinfo.getUname());
            HttpPostThread httpPostThread =   new  HttpPostThread(imageurls[position]);
            httpPostThread.setback(new HttpPostThread.Imageback() {
                @Override
                public void getback(Bitmap bitmap) {
                    viewHolder.imageview.setImageBitmap(bitmap);
                }
            });
            ThreadPoolUtils.execute(httpPostThread);
            return view;
        }


        class  Back implements HttpPostThread.Imageback
        {

            @Override
            public void getback(Bitmap bitmap) {
                Log.v("tupian",bitmap.toString());
            }
        }


    }


    public static class ViewHolder {
        public TextView textview;

        public ImageView imageview;

    }


}
